package com.lagou.web;

import com.lagou.domin.Student;
import com.lagou.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.*;
import java.util.Date;

public class AddServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从req中获取相应的值，创建student对象，传入AdminService对象中
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = null;
        try {
            birth = sdf.parse(req.getParameter("birth"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if("male".equals(sex)){
            new AdminService().add(new Student(id, name, "男", birth));
        }else{
            new AdminService().add(new Student(id, name, "女", birth));
        }
    }
}
